package co.edu.poli.ficb.agl.automatable.gui;

import java.awt.Shape;
import java.awt.geom.Line2D;
import java.awt.geom.QuadCurve2D;
import java.util.ArrayList;
import java.util.Collection;

import co.edu.poli.ficb.agl.automatable.modelo.automatas.Estado;
import co.edu.poli.ficb.agl.automatable.modelo.automatas.EstadoImpl;
import co.edu.poli.ficb.agl.automatable.modelo.automatas.TransicionImpl;
import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.graph.util.Context;
import edu.uci.ics.jung.graph.util.Pair;
import edu.uci.ics.jung.visualization.decorators.EdgeShape;
import edu.uci.ics.jung.visualization.decorators.EdgeShape.Loop;

/**
 * 
 * @author Diego Satoba
 *
 */
public class MyQuadCurve extends EdgeShape.QuadCurve<Estado, TransicionImpl>{
	
	/**
     * singleton instance of the QuadCurve shape
     */
    private static QuadCurve2D instance = new QuadCurve2D.Float();
	
	/**
     * a convenience instance for other edge shapes to use
     * for self-loop edges where parallel instances will not
     * overlay each other.
     */
    @SuppressWarnings("rawtypes")
	protected static Loop loop = new Loop();
    
    /**
     * Singleton instance of the Line2D edge shape
     */
    private static Line2D line = new Line2D.Float(0.0f, 0.0f, 1.0f, 0.0f);

    
	@SuppressWarnings("unchecked")
	@Override
	public Shape transform(Context<Graph<Estado, TransicionImpl>, TransicionImpl> context) {
		
		Graph<Estado, TransicionImpl> grafo = context.graph;
		
    	TransicionImpl regla = context.element;
    	
        Pair<Estado> endpoints = grafo.getEndpoints(regla);
        
        Collection<TransicionImpl> reglas = new ArrayList<TransicionImpl>();
        
        reglas.addAll(grafo.findEdgeSet(regla.getEstadoFuente(), regla.getEstadoDestino()));
        
        reglas.addAll(grafo.findEdgeSet(regla.getEstadoDestino(), regla.getEstadoFuente()));
        
        if (endpoints != null) {
        	boolean isLoop = endpoints.getFirst().equals(endpoints.getSecond());
        	if (isLoop) {
        		//return loop.transform(context);
        		return super.transform(context);
        	}
        }
        
        if (reglas.size() == 1)
        	return line;
        else
        	return super.transform(context);
        /*
        int index = 1;
        if (parallelEdgeIndexFunction != null) {
            index = parallelEdgeIndexFunction.getIndex(grafo, regla);
        }
        
        float controlY = control_offset_increment + control_offset_increment * index;
        
        instance.setCurve(0.0f, 0.0f, 0.5f, controlY, 1.0f, 0.0f);
        
        return instance;
        */
	}
}
